import { makeAutoObservable } from 'mobx';

export default class Auth {
  // >>> 小程序授权
  // 登录服务提供商: weixin/qq/sinaweibo/xiaomi/univerify 一键登录/apple/google/facebook
  provider = [];
  // 授权权限
  authSetting = {
    // 获取公开信息（用户名、头像等）
    userInfo: true,
    // 获取您的收货地址列表
    // ! 暂时未用到
    addressList: null,
  };
  // 小程序获取用户的授权信息 Taro.getUserInfo
  userInfoRes = {
    userInfo: {
      nickName: '',
      avatarUrl: '',
    },
    rawData: '',
    signature: '',
    encryptedData: '',
    iv: '',
  };
  // login
  oauth = {
    openid: '',
    unionid: '',
    sessionKey: null,
    id: '',
    token: '',
    createTime: null,
    updateTime: null,
    mobile: '', // 已混淆加密
    creditPoint: 0,
    grade: 0,
    expiresTime: null,
    deleted: 0,
  };
  finishLogin = null; // login 页面判断用户是否点击登录按钮，完成登录
  // 小程序授权 <<<

  constructor() {
    makeAutoObservable(this);
  }

  get userInfo() {
    return this.userInfoRes.userInfo;
  }
  get isLogged() {
    return Boolean(this.oauth.token && this.finishLogin);
  }
  get isMember() {
    return this.oauth.grade > 0 && this.oauth.expiresTime > Date.now();
  }

  setProvider(provider) {
    this.provider = provider;
  }
  setUserInfo(userInfoRes) {
    this.userInfoRes = userInfoRes;
  }
  setOauth(oauth) {
    this.oauth = oauth;
  }
  setFinishLogin() {
    this.finishLogin = 1;
  }
}
